var lastApYear = "";

var thisPage = {
    // 初始化页面
    OnPageLoad: function () {
        // 默认隐藏每月天数，采用自然天数
        $("#_day02").hide();
        // 得到最大年度
        thisPage.FetchLastApYear();
    },
    DoCreatePeriod: function () {
        // step 1：构建期间数据列表
        let PeriodList = [];
        for (let i = 0; i < parseInt($("#PeriodNum").val()); i++) {
            let Period = {};
            if ((i + 1) > 12) {
                Period['ApYear'] = parseInt(lastApYear) + 1;
                Period['ApMonth'] = 12;
            } else {
                Period['ApYear'] = parseInt(lastApYear) + 1;
                Period['ApMonth'] = i + 1;
            }
            Period['FirstDay'] = parseInt($("#FirstDay").val());
            if (parseInt($("#PeriodNum").val()) === 12) {
                Period['ApDays'] = -1;    // 此处可设置天数，-1表示采用自然月份天数
            } else {
                Period['ApDays'] = parseInt($("#PeriodDays").val());
            }
            PeriodList.push(Period);
        }
        // step 2：创建期间，并返回
        $.ajax({
            type: "POST",
            url: parent.basePath + "/financial/accouting_period/insert.html",
            timeout: 30 * 1000,
            data: JSON.stringify(PeriodList),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            parent.$("#ApYear").val(parseInt(lastApYear) + 1);
            parent._Instance.FetchForPage(1);
            closePopupWnd();
        }).fail(function (data) {
            ajaxError(data, 1);
            $(".error-msg").css({top: '8px', left: '130px'});
        }).always(function ( ) {
        });
    },
    FetchLastApYear: function () {
        $.ajax({
            type: "POST",
            url: parent.basePath + "/financial/accouting_period/fetchLastApYear.html",
            timeout: 30 * 1000,
            data: {},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            lastApYear = data.LastApYear;
            $("#ApYear").val(parseInt(lastApYear) + 1);
            $('#loading').hide();
        }).fail(function (data) {
            ajaxError(data, 1);
            $(".error-msg").css({top: '8px', left: '130px'});
            $('#loading').hide();
        }).always(function ( ) {
        });
    },
    ShowPeriodDays: function () {
        if ($("#PeriodNum").val() === "13") {
            $("#_day01").hide();
            $("#_day02").show();
        } else {
            $("#_day01").show();
            $("#_day02").hide();
        }
    }
};

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    thisPage.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
}




